package org.xuyuji.DBtoBean.data;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.xuyuji.DBtoBean.common.TypeMap;

public class TableInfo {
	
	private String tableName;
	
	private String className;
	
	List<Map<String,String>> columnList = new ArrayList<Map<String, String>>();
	
	private static Map<String, TableInfo> buffer = new HashMap<String, TableInfo>();
	
	private TableInfo(){}
	
	public static TableInfo valueOf(String tableName)	throws Exception {
		if(buffer.containsKey(tableName)){
			return buffer.get(tableName);
		}else{
			TableInfo tableInfo = new TableInfo().fillData(tableName);
			buffer.put(tableName, tableInfo);
			return tableInfo;
		}
	}

	private TableInfo fillData(String tableName) throws Exception{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.201:1521:dev1ora", "iccard", "iccard");
		DatabaseMetaData dm = con.getMetaData();

		ResultSet colRet = dm.getColumns(null, "%", tableName, "%");
		while (colRet.next()) {
			Map<String, String> columnInfo = new HashMap<String, String>();
			String columnName = colRet.getString("COLUMN_NAME");
			String columnType = colRet.getString("TYPE_NAME");
			String beanColumnName = toBeanName(columnName);
			
			columnInfo.put("cName", columnName);
			columnInfo.put("cType", columnType);
			columnInfo.put("pName", beanColumnName);
			columnInfo.put("pType", toBeanType(columnType));
			columnInfo.put("pReader", "get" + beanColumnName.substring(0, 1).toUpperCase() + beanColumnName.substring(1, beanColumnName.length()));
			columnInfo.put("pWriter", "set" + beanColumnName.substring(0, 1).toUpperCase() + beanColumnName.substring(1, beanColumnName.length()));
			
			columnList.add(columnInfo);
		}
		
		this.tableName = tableName;
		className = toBeanName(tableName);
		className = className.substring(0, 1).toUpperCase() + className.substring(1, className.length());
		
		return this;
	}
	
	private String toBeanName(String columnName){
		String columnNameLowerCase = columnName.toLowerCase();
		StringBuilder beanName = new StringBuilder();
		
		String word = "";
		for(int i = 0; i < columnNameLowerCase.length(); i++){
			word = columnNameLowerCase.substring(i, i + 1);
			if("_".equals(word)){
				i++;
				word = columnNameLowerCase.substring(i, i + 1).toUpperCase();
			}
			beanName.append(word);
		}
		
		return beanName.toString();
	}
	
	private String toBeanType(String columnType) throws Exception{
		String columnTypeLowerCase = columnType.toLowerCase();
		
		String beanType = TypeMap.get(columnTypeLowerCase);
		
		if(StringUtils.isEmpty(beanType)){
			throw new Exception("不支持的类型:" + columnType);
		}
		
		return beanType;
	}

	public String getTableName() {
		return tableName;
	}

	public String getClassName() {
		return className;
	}

	public List<Map<String, String>> getColumnList() {
		return columnList;
	}
}
